package com.wenresearch.orion.components.ui.accordion;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.container.VerticalFieldManager;

/**
 * Ui Accordion
 * Ui Accordion can be added to UiAccordionPanel
 *  
 * @author Ali Irawan (boylevantz@gmail.com)
 * @version 1.0
 * @see com.wenresearch.orion.components.ui.accordion.UiAccordion
 */
public class UiAccordion extends VerticalFieldManager {

	private AccordionHeader _header;
	private Manager _content;
	private Bitmap _bitmapPlus;
	private Bitmap _bitmapMinus;

	/**
	 * Create an accordion with specified title and content
	 * @param title title to be shown
	 * @param content content manager
	 */
	public UiAccordion(String title, Manager content) {
		super(FOCUSABLE);

		_header = new AccordionHeader(title, true);
		_content = content;

		_bitmapPlus = Bitmap.getBitmapResource("img/plus.png");
		_bitmapMinus = Bitmap.getBitmapResource("img/minus.png");

		add(_header);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.rim.device.api.ui.Manager#paint(net.rim.device.api.ui.Graphics)
	 */
	protected void paint(Graphics graphics) {
		super.paint(graphics);
		// Nothing to do ad the moment
	}

	/**
	 * Internal class representing Accordionheader
	 * 
	 * @author Ali Irawan (boylevantz@gmail.com)
	 *
	 */
	private final class AccordionHeader extends Field {
		private String _title;
		private boolean _collapse;

		/**
		 * Create accordion header with title and collapse default state
		 * @param title
		 * @param collapse
		 */
		public AccordionHeader(String title, boolean collapse) {
			super(USE_ALL_WIDTH | FOCUSABLE);
			_title = title;
			_collapse = collapse;
		}

		/**
		 * Get title
		 * @return string title
		 */
		public String getTitle() {
			return _title;
		}

		/**
		 * Set title
		 * @param title set title to be shown
		 */
		public void setTitle(String title) {
			_title = title;
		}

		/**
		 * Get collapse status
		 * @return true when the accordion is in collapse state
		 */
		public boolean isCollapse() {
			return _collapse;
		}

		/**
		 * Set collapse status
		 * 
		 * @param collapse set whether the accordion collapse or not
		 */
		public void setCollapse(boolean collapse) {
			_collapse = collapse;
			if (_collapse) {
				try {
					delete(_content);
				} catch (Exception ex) {
				}
			} else {
				add(_content);
			}
		}

		/**
		 * Set width and height
		 */
		protected void layout(int width, int height) {
			int fontHeight = getFont().getHeight();
			setExtent(width, fontHeight + 10);
		}

		/**
		 * Draw the accordion
		 */
		protected void paint(Graphics graphics) {

			graphics.drawLine(0, 0, getWidth(), 0);

			if (isCollapse()) {
				if (_bitmapPlus != null) {
					graphics.drawBitmap(5, (getHeight() - _bitmapMinus.getHeight()) / 2, _bitmapPlus.getWidth(),
							_bitmapPlus.getHeight(), _bitmapPlus, 0, 0);
				}
			} else {
				if (_bitmapMinus != null) {
					graphics.drawBitmap(5, (getHeight() - _bitmapMinus.getHeight()) / 2, _bitmapMinus.getWidth(),
							_bitmapMinus.getHeight(), _bitmapMinus, 0, 0);
				}
			}
			graphics.drawText(getTitle(), 30, 5);
			graphics.drawLine(0, getHeight(), getWidth(), getHeight());
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see net.rim.device.api.ui.Field#navigationClick(int, int)
		 */
		protected boolean navigationClick(int status, int time) {
			// TODO Auto-generated method stub
			setCollapse(!isCollapse());
			invalidate();
			return true;
		}

	}

	/**
	 * Change collapse icon, default icon is on res/img/plus.png
	 * @param icon icon object to specify
	 */
	public void setCollapseIcon(Bitmap icon) {
		_bitmapPlus = icon;
	}

	/**
	 * Change collapse icon, default icon is on res/img/minus.png
	 * @param icon icon object to specify
	 */
	public void setExpandIcon(Bitmap icon) {
		_bitmapMinus = icon;
	}
	
	/**
	 * Get the accordion content manager
	 * @return content manager inside accordion
	 */
	public Manager getContent() {
		return _content;
	}
}
